package com.eightbears.beboy.adapter;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ToggleButton;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.eightbears.beboy.R;
import com.eightbears.beboy.base.util.CommonUtils;
import com.eightbears.beboy.base.util.SPUtil;
import com.eightbears.beboy.entity.ManagerEntity;
import com.eightbears.beboy.entity.UserEntity;

import static com.eightbears.beboy.entity.UserEntity.LOCATION_MANAGER;
import static com.eightbears.beboy.entity.UserEntity.LOCATION_SHOP_MANAGER;
import static com.eightbears.beboy.ui.merchant.activity.ShopPowerManageActivity.STATUS_OPEN;

/**
 * Created by Administrator on 2017/9/4.
 */

public class ShopPowerAdapter extends BaseQuickAdapter<ManagerEntity.Manager, BaseViewHolder> {

    private UserEntity userEntity;


    public ShopPowerAdapter() {
        super(R.layout.item_shop_power);
        if (userEntity == null) {
            userEntity = SPUtil.getUser();
        }
    }

    private ToggleButton tb_sel;
    private LinearLayout ll_edit_name;

    @Override
    protected void convert(BaseViewHolder baseViewHolder, ManagerEntity.Manager manager) {
        tb_sel = baseViewHolder.getView(R.id.tb_sel);
        ll_edit_name = baseViewHolder.getView(R.id.ll_edit_name);

        // 根据身份显示ui
        if (userEntity.getData().getShops().get(0).getLevel().equals(LOCATION_MANAGER)) {
            if (userEntity.getData().getUser_id().equals(manager.getUser_id())) {
                showMyEdit();
            } else {
                hindShopManagerPower();
            }
        } else {
            if (userEntity.getData().getUser_id().equals(manager.getUser_id())
                    || manager.getLevel().equals(LOCATION_SHOP_MANAGER)) {
                showMyEdit();
            } else {
                showShopManagerPower();
            }
        }

        // 经理授权按钮
        if (manager.getStatus().equals(STATUS_OPEN))
            tb_sel.setChecked(true);
        else
            tb_sel.setChecked(false);

        CommonUtils.loadImageView(mContext, manager.getUser().getCover(),
                R.mipmap.default_head, ((ImageView) baseViewHolder.getView(R.id.iv_logo)));
        baseViewHolder.setText(R.id.tv_edit_name, manager.getName())
                .setText(R.id.tv_nickname, manager.getUser().getNickname())
                .addOnClickListener(R.id.ll_edit_name)
                .addOnClickListener(R.id.tb_sel);

    }


    private void showShopManagerPower() {
        tb_sel.setVisibility(View.VISIBLE);
        ll_edit_name.setVisibility(View.VISIBLE);
    }

    private void hindShopManagerPower() {
        tb_sel.setVisibility(View.GONE);
        ll_edit_name.setVisibility(View.GONE);
    }

    private void showMyEdit() {
        tb_sel.setVisibility(View.GONE);
        ll_edit_name.setVisibility(View.VISIBLE);
    }

}
